from django.shortcuts import render

# Create your views here.
from rest_framework.authentication import BasicAuthentication, SessionAuthentication, TokenAuthentication
from rest_framework.filters import SearchFilter
from rest_framework.mixins import ListModelMixin,CreateModelMixin, UpdateModelMixin, RetrieveModelMixin
from rest_framework.pagination import LimitOffsetPagination, PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import GenericViewSet
from rest_framework_simplejwt.authentication import JWTAuthentication

from movie.models import Movie
from movie.serializers import MovieListSerializer


# 自定义分页器
class MoviePagination(PageNumberPagination):
    # 每页数量
    page_size = 5
    # 请求时将page改名为p
    page_query_param = 'p'
    # 最大可请求页数
    max_page_size = 20


class MovieListViewSet(ListModelMixin, GenericViewSet):

    queryset = Movie.objects.all()
    serializer_class = MovieListSerializer

    # 过滤器可多个
    filter_backends = [SearchFilter,]

    # 搜索过滤字段设置
    search_fields = ['showname', ]

    # 添加分页
    pagination_class = MoviePagination

    # 局部添加定制身份权限认证
    # 设置需要身份验证的类
    authentication_classes = [BasicAuthentication, SessionAuthentication, JWTAuthentication]
    # 全类权限认证
    permission_classes = [IsAuthenticated, ]
